package com.colin.pojo;

import static java.lang.Math.min;
import static java.lang.StrictMath.max;

public class Suanfa {
    private int timeMatrix[][] = new int[10][10];// 作业i的第j道工序花费的时间
    private int machineMatrix[][] = new int[10][10]; // 作业i的第j道工序在哪个机器上运作
    private int process[] = new int[10];//作业有多少道工序
    private int totalTime[] = new int[10];//totalTime[i] 第i 个机器花费的总时间
    private int lufe[] = new int[10];
    private int zuoluo[] = new int[10];//代表作业i进行到了第几道工序;
    private int resultSet[] = new int[100];//结果集合
    private int sum;
    private int machinenumber;
    private int productnumber;       //工序总数    机器数   产品数
    private int upworld=50;             //上界

    public int[][] getTimeMatrix () {
        return timeMatrix;
    }

    public void setTimeMatrix (int[][] timeMatrix) {
        this.timeMatrix = timeMatrix;
    }

    public int[][] getMachineMatrix () {
        return machineMatrix;
    }

    public void setMachineMatrix (int[][] machineMatrix) {
        this.machineMatrix = machineMatrix;
    }

    public int[] getProcess () {
        return process;
    }

    public void setProcess (int[] process) {
        this.process = process;
    }

    public int[] getTotalTime () {
        return totalTime;
    }

    public void setTotalTime (int[] totalTime) {
        this.totalTime = totalTime;
    }

    public int[] getLufe () {
        return lufe;
    }

    public void setLufe (int[] lufe) {
        this.lufe = lufe;
    }

    public int[] getZuoluo () {
        return zuoluo;
    }

    public void setZuoluo (int[] zuoluo) {
        this.zuoluo = zuoluo;
    }

    public int[] getResultSet () {
        return resultSet;
    }

    public void setResultSet (int[] resultSet) {
        this.resultSet = resultSet;
    }

    public int getSum () {
        return sum;
    }

    public void setSum (int sum) {
        this.sum = sum;
    }

    public int getMachinenumber () {
        return machinenumber;
    }

    public void setMachinenumber (int machinenumber) {
        this.machinenumber = machinenumber;
    }

    public int getProductnumber () {
        return productnumber;
    }

    public void setProductnumber (int productnumber) {
        this.productnumber = productnumber;
    }

    public int getUpworld () {
        return upworld;
    }

    public void setUpworld (int upworld) {
        this.upworld = upworld;
    }

    public  void Greedy (int farmer) {
        int black = 0;
        for (int i = 1; i <= machinenumber; i++) black = max(black, totalTime[i]);     //获取当前全部机器工作时间最长的值
        if (black > upworld) return;               //如果该值超过上界 则砍掉该分支
        if (farmer == sum) {

            //如果执行完全部工序 则输出可行解
            for (int i = 1; i <= farmer; i++)
                System.out.print(resultSet[i] + " ");
            System.out.println();
            upworld = min(upworld, black);        //缩小上界
            return;
        }
        else {
            for (int i = 1; i <= productnumber; i++) {
                if (zuoluo[i] == process[i-1]) {//作业i已经执行完全部工序 跳过该作业
                    continue;
                }

                else {
                    int w = zuoluo[i];       //获取当前产品(i)执行的工序==1
                    zuoluo[i]++;           //产品(i)继续向下执行工序
                    int p = totalTime[machineMatrix[i][zuoluo[i]]];    //机器(totalTime)的生产 作业(i)工序(zuoluo[i])的时间==3,
                    int q = lufe[i];//0
                    if (totalTime[machineMatrix[i][zuoluo[i]]] <= lufe[i]) {    //进行比较
                        totalTime[machineMatrix[i][zuoluo[i]]] = lufe[i] + timeMatrix[i][zuoluo[i]];
                    }
                    else {
                        totalTime[machineMatrix[i][zuoluo[i]]] += timeMatrix[i][zuoluo[i]];
                    }
                    lufe[i] = totalTime[machineMatrix[i][zuoluo[i]]];                  //将机器(totalTime)的生产 作业(i)
                    // 当前工序花费的总时间 付给tmp[i]
                    resultSet[farmer + 1] = i;           //将作业i放入结果集
                    Greedy(farmer + 1);             //完成工序数+1 进行深度搜索
                    //将全局变量返回到当时状态
                    lufe[i] = q;
                    zuoluo[i] = w;
                    totalTime[machineMatrix[i][zuoluo[i] + 1]] = p;
                }
            }
        }
    }
}
